<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-7
 * Time: 上午9:54
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Balance extends Modules_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->rules = array(
            "rule" => array(
                array(
                    "field" => "title",
                    "label" => lang('title'),
                    "rules" => "trim|required|min_length[1]"
                )
            ,array(
                    "field" => "timeline",
                    "label" => lang('time'),
                    "rules" => "trim|strtotime"
                )
            ,array(
                    "field" => "content",
                    "label" => lang('conent'),
                    "rules" => "trim"
                    // link_create tag 生成
                )
            ,array(
                    "field" => "photo",
                    "label" => lang('photo'),
                    "rules" => "trim"
                )
            )
        );

    }

    public function _edit_data()
    {
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }
    public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];

        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        $order = $this->_index_orders();
        if ($this->input->get('order',TRUE)) {
            // TODO: order
            // $orders = explode("-",$this->input->get('order',TRUE));
            $order = $this->input->get('order',TRUE);
        }
        $where_in = array("balance_detail.cid"=>$this->cid,"balance_detail.ccid"=>$this->ccid);
        // 条件必须
        $where = array_merge($this->_index_where(),$where_in);
        $count = $this->model->get_count_all($where,false,array('account','account.id=balance_detail.uid','left'));
        $vdata['pages'] = $this->_pages_join(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$count,4);
        //$vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$where,4);
        $fields = 'balance_detail.*,account.uname,account.phone';
        $vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where,$fields,false,array('account','account.id=balance_detail.uid','left'));
        foreach($vdata['list'] as &$row) {
            $row = $this->model->status_desc($row);
        }

        $this->_display($vdata);
    }
    protected function _index_where(){
        $arr =array();
        if (isset($_GET['ctype']) && $_GET['ctype']) {
            $arr['ctype'] = $_GET['ctype'];
        }
        if (isset($_GET['uname']) && $_GET['uname']) {
            $arr['like 1'] = array('account.uname',$_GET['uname']);
            //$arr['account.old_com_name'] = $_GET['old_com_name'];
        }
        if (isset($_GET['phone']) && $_GET['phone']) {
            $arr['like 2'] = array('account.phone',$_GET['phone']);
            //$arr['account.com_name'] = $_GET['com_name'];
        }
        if (isset($_GET['type']) && $_GET['type']) {
            $arr['balance_detail.type'] = $_GET['type'];
            //$arr['account.old_com_name'] = $_GET['old_com_name'];
        }
        if (isset($_GET['pay_ways']) && $_GET['pay_ways']) {
            $arr['balance_detail.pay_ways'] = $_GET['pay_ways'];
            //$arr['account.old_com_name'] = $_GET['old_com_name'];
        }
        if (isset($_GET['payment_sn']) && $_GET['payment_sn']) {
            $arr['balance_detail.payment_sn'] = $_GET['payment_sn'];
            //$arr['account.old_com_name'] = $_GET['old_com_name'];
        }
        return $arr;
    }
    /**
     * @brief 默认编辑页面
     */
    protected function edit($key=false){
        if (!$key) {
            $key = $this->input->get_post('id',TRUE);
            if ($this->input->is_ajax_request()){
                if (!$key) {
                    $vdata = array('msg'=>'没有提供标识','status'=>0);
                    $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
                }
            }else{
                if (!$key) {
                    if (isset($this->cid)) {
                        $index = '/index/'.$this->cid;
                    }else{
                        $index = '/index';
                    }
                    redirect(site_url($this->class.$index));
                }
            }
        }
        $this->form_validation->set_rules($this->_get_rule('edit'));
        if ($this->form_validation->run() == false) {
            $vdata['it'] = $this->model->get_one($key);

            if (!$vdata['it']) {
                $vdata = array('msg'=>'提供的标示是不存在的','status'=>0);
                if ($this->input->is_ajax_request()) {
                    $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
                }else{
                    $this->load->view('msg',$vdata);
                    return false;
                }
            }
            $vdata['it'] = $this->model->status_desc($vdata['it']);
            $vdata['it']['account'] = $this->macc->get_one($vdata['it']['uid'],'uname,phone');
            if ($this->input->is_ajax_request()) {
                if (is_post()) {
                    $vdata['status'] = 0;
                    $vdata['msg'] = validation_errors();
                }
                $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
            }else{
                $this->_display($vdata);
            }
        }else{
            $this->_edit();
        }
    }
}
